package com.yhp.web;

import com.yhp.bean.Role;
import com.yhp.bean.Users;

import com.yhp.service.RoleService;
import com.yhp.service.UsersService;
import com.yhp.service.impl.RoleServiceImpl;
import com.yhp.service.impl.UsersServiceImpl;
import com.yhp.util.PageUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet(urlPatterns = {"/power/user/users"})
public class UsersServlet extends HttpServlet {
    private UsersService usersService=new UsersServiceImpl();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getParameter("method");
        if("select".equals(method)){
            select(req,resp);
        }else if ("findbyid".equals(method)){
            findbyid(req,resp);
        }else if ("insert".equals(method)){
            insert(req,resp);
        }else if ("update".equals(method)){
            update(req,resp);
        }else if ("delete".equals(method)){
            delete(req,resp);
        }
    }

    /**
     * 新增
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void insert(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String loginname = req.getParameter("loginname");
        String password = req.getParameter("password");
        String realname = req.getParameter("realname");
        String sex = req.getParameter("sex");
        String email = req.getParameter("email");
        String address = req.getParameter("address");
        String phone = req.getParameter("phone");
        String cardid = req.getParameter("cardid");
        String descs = req.getParameter("descs");
        String roleid = req.getParameter("roleid");
        //调取service
        UsersService service = new UsersServiceImpl();
        //将参数封装到用户对象中
        Users users = new Users();
        users.setLoginName(loginname);
        users.setPassWord(password);
        users.setRealName(realname);
        users.setSex(Integer.parseInt(sex));
        users.setEmail(email);
        users.setAddress(address);
        users.setPhone(phone);
        users.setCardId(cardid);
        users.setDescs(descs);
        users.setRoleId(Integer.parseInt(roleid));
        int i = service.insertUser(users);
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        if(i>0){
            writer.println("<script>alert('新增成功');location.href='/power/user/users?method=select'</script>");
        }else{
            writer.println("<script>alert('新增失败');location.href='/Educational/student/getGradeList'</script>");
        }


    }
    protected void select(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.接收参数(分页参数+模糊查参数)
        String index = req.getParameter("index");
        int pageIndex=(index==null||index.length()==0)?1:Integer.parseInt(index);
        //2.调取service方法(1.查询数据列表的方法  2.查询总条数的方法)
        PageUtil pageUtil = new PageUtil();

        List<Users> usersList = usersService.getUsersList(pageIndex, pageUtil.getPageSize());
        int total = usersService.total();
        pageUtil.setTotal(total);
        pageUtil.setPageIndex(pageIndex);
        pageUtil.setDataList(usersList);

        //3.存值跳页面
        req.setAttribute("pi",pageUtil);
        req.getRequestDispatcher("list.jsp").forward(req,resp);
    }

    /**
     * 主键查询
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void findbyid(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String uid = req.getParameter("uid");
        //根据主键查询用户信息
        UsersService service = new UsersServiceImpl();
        Users users = service.findById(Integer.parseInt(uid));

        //查询角色
        RoleService roleService = new RoleServiceImpl();
        List<Role> list = roleService.getList();
        req.setAttribute("rlist", list);
        req.setAttribute("user", users);
        req.getRequestDispatcher("edit.jsp").forward(req,resp);
    }

    protected void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String uid = req.getParameter("uid");
        String loginname = req.getParameter("loginname");
        String password = req.getParameter("password");
        String realname = req.getParameter("realname");
        String sex = req.getParameter("sex");
        String email = req.getParameter("email");
        String address = req.getParameter("address");
        String phone = req.getParameter("phone");
        String cardid = req.getParameter("cardId");
        String descs = req.getParameter("descs");
        String roleid = req.getParameter("roleid");
        Users users = new Users();
        users.setUserId(Integer.parseInt(uid));
        users.setLoginName(loginname);
        users.setPassWord(password);
        users.setRealName(realname);
        users.setSex(Integer.parseInt(sex));
        users.setEmail(email);
        users.setAddress(address);
        users.setPhone(phone);
        users.setCardId(cardid);
        users.setDescs(descs);
        users.setRoleId(Integer.parseInt(roleid));
        UsersService usersService = new UsersServiceImpl();
        int i = usersService.updateUser(users);
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        if(i>0){
            writer.println("<script>alert('更新成功');location.href='/power/user/users?method=select'</script>");
        }else{
            writer.println("<script>alert('更新失败');location.href='/power/user/users?method=findbyid&uid="+uid+"'</script>");
        }

    }

    protected void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String uid = req.getParameter("uid");
        UsersService usersService = new UsersServiceImpl();
        int users = usersService.delUser(Integer.parseInt(uid));
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        if(users>0){
            writer.println("<script>alert('删除成功');location.href='/power/user/users?method=select'</script>");
        }else{
            writer.println("<script>alert('删除失败');location.href='/power/user/users?method=select'</script>");
        }
    }

}
